package com.cskaoyan._07object._01demo;

import java.util.Objects;

/**
 * @author Macro_Ray
 * @since 2024/3/15 11:43
 */
// equals
public class Equals {
    public static void main(String[] args) {
        Test t1 = new Test("zhangsan", 20);
        Test t2 = new Test("zhangsan", 20);
        Test t3 = new Test("lisi", 26);

        System.out.println(t1.equals(t2)); // true
        System.out.println(t1.equals(t3)); // false

        // 1.如果没有重写equals方法，那么默认使用object中的方法进行比较，比较的是地址值是否相等
        // 2.一般来讲地址值对于我们意义不大，所以我们会重写，重写之后比较的就是对象内部的属性值了

    }
}

class Test {
    String name;
    int age;

    public Test() {
    }

    public Test(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Test test = (Test) o;
        return age == test.age && Objects.equals(name, test.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
